

#import "TestStackViewController.h"
#import "TSView.h"



@interface TestStackViewController ()

@property (nonatomic, strong) UIStackView *containerView;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UIButton *reduceBtn;
@property (nonatomic, strong) NSArray<UILabel *> *labels;

@end

@implementation TestStackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.labels = [NSArray array];
    [self.view addSubview:self.addBtn];
    [self.view addSubview:self.reduceBtn];
    [self test2];
    [self addDismissButton];
}

- (void)test2 {
    self.containerView = [[UIStackView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 200)];
    self.containerView.axis = UILayoutConstraintAxisHorizontal;
    self.containerView.distribution = UIStackViewDistributionEqualCentering;
    // UIStackViewDistributionFillEqually StackView 减去所有spacing后均分给 arrangedSubviews
    // UIStackViewDistributionFillProportionally 根据 arrangedSubviews 的 intrinsicContentSize 按比例分配。最后一个view突出的小
    // UIStackViewDistributionEqualSpacing 这种是使 arrangedSubview 之间的spacing相等，但是这个spacing是有可能大于 StackView 所设置的spacing，但是绝对不会小于。这个类型的布局可以这样理解，先按所有的 arrangedSubview 的 intrinsicContentSize 布局，然后余下的空间均分为spacing，如果大约 StackView 设置的spacing那这样就OK了，如果小于就按照 StackView 设置的spacing，然后按照 CompressionResistance 的优先级来压缩一个 arrangedSubview 。
    // UIStackViewDistributionEqualCentering
    
    self.containerView.spacing = 10;
    self.containerView.alignment = UIStackViewAlignmentLeading;
    // UIStackViewAlignmentFill, 默认方式， 如果子控件水平布局, 则指子控件的垂直方向填充满stackView. 反之亦然
    // UIStackViewAlignmentLeading, 如果子控件竖直布局, 则指子控件左边对齐stackView左边. 反之亦然, 即 UIStackViewAlignmentTop = UIStackViewAlignmentLeading。
    // UIStackViewAlignmentFirstBaseline, 根据上方基线布局所有子视图的y值(适用于Horizontal模式)， 这种模式没搞懂， 有知道怎么回事的求教了
    // UIStackViewAlignmentLastBaseline, 根据下方基线布局所有子视图的y值(适用于Horizontal模式)
    // UIStackViewAlignmentCenter, 中心对齐
    // UIStackViewAlignmentTrailing, 如果子控件竖直布局, 则指子控件左边对齐stackView右边. 反之亦然, 即UIStackViewAlignmentBottom = UIStackViewAlignmentTrailing
    // UIStackViewAlignmentBottom = UIStackViewAlignmentTrailing,
    
    [self.view addSubview:self.containerView];
}

- (void)test1 {
    self.containerView = [[UIStackView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 200)];
    self.containerView.axis = UILayoutConstraintAxisHorizontal;
    self.containerView.distribution = UIStackViewDistributionFillEqually;
    self.containerView.spacing = 10;
    self.containerView.alignment = UIStackViewAlignmentFill;
    
    for (NSInteger i = 0; i < 4; i++) {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor colorWithRed:random()%256/255.0 green:random()%256/255.0 blue:random()%256/255.0 alpha:1.0];
        [self.containerView addArrangedSubview:view];
    }
    [self.view addSubview:self.containerView];
    /*
     addArrangedSubview 和 insertArrangedSubview， 会把子控件加到arrangedSubviews数组的同时添加到StackView上，
     但是removeArrangedSubview， 只会把子控件从arrangedSubviews数组中移除，
     不会从subviews中移除，如果需要可调用removeFromSuperview
     1 axis：    子控件的布局方向，水平或垂直， 这个不用过多解释了
     2 spacing：     子控件之间的最小间距，之所以说是最小间距，因为stackView会根据一定的规则对内部空间布局，有的时候不能满足所有要求，比如stackView 本身宽度100，内部两个控件，宽度都为50，50+50+10就超过了本身宽度， 这时会压缩其中一个子控件的宽度来满足最小间距。
     3 distribution：子控件依据何种规则布局， 这个比较抽象， 看示例理解的快一点，以下示例均使用UILabel演示， 原因见插播
     */
}



- (void)addBtnClick:(UIButton *)sender {
    UILabel *label = [[UILabel alloc] init];
    label.text = @"这是一段测试文字";
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:16];
    label.backgroundColor = [UIColor colorWithRed:random()%256/255.0 green:random()%256/255.0 blue:random()%256/255.0 alpha:1.0];
    [self labelsAddObject:label];
    [self.containerView addArrangedSubview:label];
}
- (void)reduceBtnClick:(UIButton *)sender {
    UILabel *lable = [self labelsRemoveLast];
    [self.containerView removeArrangedSubview:lable];
    [lable removeFromSuperview];
}
- (UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [[UIButton alloc] init];
        _addBtn.frame = CGRectMake(0, 400, 150, 44);
        [_addBtn setTitle:@"加一个lable" forState:UIControlStateNormal];
        [_addBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}
- (UIButton *)reduceBtn {
    if (!_reduceBtn) {
        _reduceBtn = [[UIButton alloc] init];
        _reduceBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 150, 400, 150, 44);
        [_reduceBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_reduceBtn setTitle:@"减一个lable" forState:UIControlStateNormal];
        [_reduceBtn addTarget:self action:@selector(reduceBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reduceBtn;
}

- (void)labelsAddObject:(UILabel *)lable {
    NSMutableArray *arr = [NSMutableArray arrayWithArray:self.labels];
    [arr addObject:lable];
    self.labels = arr.copy;
}
- (UILabel *)labelsRemoveLast {
    NSMutableArray *arr = [NSMutableArray arrayWithArray:self.labels];
    if (arr.count > 0) {
        UILabel *label = arr.lastObject;
        [arr removeObject:label];
        self.labels = [arr copy];
        return label;
    }
    return nil;
}

/*
 // 设置 UIView 的 Intrinsic Content Size
 - (CGSize)intrinsicContentSize {
 CGSize originalSize = [super intrinsicContentSize];
 CGSize size = CGSizeMake(originalSize.width + 20, originalSize.height + 20);
 return size;
 }
 */

- (void)addDismissButton {
    UIButton *dismiss = ({
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitle:@"dismiss" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        CGFloat f = 100;
        btn.frame = CGRectMake(SCREEN_WIDTH - f, SCREEN_HEIGHT - f, f, f);
        [btn setBackgroundColor:[UIColor purpleColor]];
        [btn addTarget:self action:@selector(dismissButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn;
    });
    [self.view addSubview:dismiss];
}
- (void)dismissButtonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:true completion:nil];
}

@end
